// https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock-with-cooldown/
// 309. Best Time to Buy and Sell Stock with Cooldown
// You are given an array prices where prices[i] is the price of a given stock on
// the ith day
// Find the maximum profit you can achieve. You may complete as many transactions as
// you like with the following restrictions:
// - After you sell stocks, you cannot buy stock on the next day (cooldown one day)
// Note: you may not engage in multiple transactions simultaneously (you must sell 
// the stock before you buy again)
function maxProfit(prices: number[]): number {
    if (prices.length === 0) return 0;

    const n = prices.length;
    const dp = new Array(n).fill(0).map(() => new Array(3).fill(0));
    dp[0][0] = -prices[0];

    for (let i = 1; i < n; i++) {
        dp[i][0] = Math.max(dp[i-1][0], dp[i-1][2] - prices[i]);
        dp[i][1] = dp[i-1][0] + prices[i];
        dp[i][2] = Math.max(dp[i-1][2], dp[i-1][1]);
    }

    return Math.max(dp[n-1][1], dp[n-1][2]);
}
